import { IRefPoolObj, RefPool } from "../util/Pool";
import { ECSCmp } from "./ECSCmp";

/**
 * [ECS]实体组件(走引用计数对象池)
 */
export abstract class ECSRefPoolCmp extends ECSCmp implements IRefPoolObj {
	ref: number;
	abstract get pool(): RefPool<ECSRefPoolCmp>;

	/**
	 * 添加引用
	 */
	use() {
		++this.ref;
	}

	/**
	 * 解除引用
	 * @param free 计数为0时是否回收
	 */
	unuse(free = true) {
		if (free) {
			this.pool.free(this);
		} else {
			--this.ref;
		}
	}
}
